package com.hawk.eye.common.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author salad
 */
@Data
public class PageResult<T> {

    /**
     * 最大页行数
     */
    public static final int MAX_PAGE_SIZE = 2000;

    /**
     *
     */
    private Long pageNum;

    /**
     * 页行数
     */
    private Long pageSize;

    /**
     * 总行数
     */
    private Long total;

    /**
     * 页数
     */
    private Long pages;

    /**
     * 当前页数据
     */
    private List<T> records;

    public PageResult(Long pageNum, Long pageSize, Long total, List<T> records) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.pages = total <= pageSize ? 1L : total / pageSize;
        this.records = records;
    }

    public PageResult(Long pageNum, Long pageSize, Long total, Long pages, List<T> records) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.pages = pages;
        this.records = records;
    }

    public static <T> PageResult<T> of(IPage<T> page) {
        return new PageResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getPages(),
                page.getRecords());
    }

    public static <T, R> PageResult<R> of(IPage<T> page, Function<T, R> mapper) {
        return new PageResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getPages(),
                page.getRecords().stream().map(mapper).collect(Collectors.toList()));
    }

    public static <T, R> PageResult<R> of(PageResult<T> page, Function<T, R> mapper) {
        return new PageResult<>(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getPages(),
                page.getRecords().stream().map(mapper).collect(Collectors.toList()));
    }



}
